﻿
var controller = this.lookupController();
var status = controller.analysis().status;
var expenseType = ref.ExpenseType.getValue();
var budgetitemId = ref.BudgetItems.getValue();
var date = new Date();
var year = date.getFullYear();
var balance = 0;
var item;
var budgetControl = "N";
var grid2 = ref.datagrid2,
    store2 = grid2.getStore();
var grid3 = ref.datagrid3
var store3 = grid3.getStore();
var grid4 = ref.datagrid4,
    editor4 = grid4.getColumns()[0].getEditor && grid4.getColumns()[0].getEditor();
var grid5 = ref.datagrid5,
    editor5 = grid5.getColumns()[0].getEditor && grid5.getColumns()[0].getEditor();

if ((status == 'launch' || status == 'resubmit' || status == '重新提交') && editor4 && editor5) {
    var record = store.getUpdatedRecords()[0],
        flag = 'update';

    if (!record) {
        record = store.getNewRecords()[0];
        flag = 'add';
    }

    if (!record) {
        flag = 'reset';
    }

    if (record) {
        if (flag == 'update') {
            if (record.modified) {
                var pro = record.get("ProjectActivity");
                //console.log(record.modified.CompanySharingRatio);
                var companySharingRatio = record.get("CompanySharingRatio");
                if (companySharingRatio != '') {
                    if (parseFloat(companySharingRatio) < 0 || parseFloat(companySharingRatio) > 100) {
                        record.set("CompanySharingRatio", "");
                    }
                }

                if (record.modified.CompanySharingRatio !== undefined) {
                    Ext.PAIRest.request({
                        ns: PAI.application.serviceUrl,
                        url: 'budget-rest/api/budgetFmebugetItem/page',
                        method: "POST",
                        async: false,
                        data: {
                            "projectactivity": record.get("ProjectActivity"),
                            "budgetitemId": budgetitemId,
                            "yearVal": year
                        },
                        success: function (data) {
                            if (data.budgetFmebugetItemF04RespS01s.length > 0) {
                                balance = data.budgetFmebugetItemF04RespS01s[0].balance;
                                budgetControl = data.budgetFmebugetItemF04RespS01s[0].budgetControl;
                            } else {
                                balance = 0;
                            }
                        },
                        failure: function (jsondata) {
                        }
                    });

                }

                if (record.modified.DescriptionOfSuppliesServices !== undefined) {
                    var v = store2.findRecord('DescriptionOfSuppliesServices', record.data.DescriptionOfSuppliesServices);
                    record.data.ProjectActivity = v.data.ProjectActivity;


                    if (record.modified.DescriptionOfSuppliesServices === '') {
                        if (record.data.Company === 'JLRC') {
                            editor4.getStore().add({
                                // id:record.data.id,
                                name: record.data.DescriptionOfSuppliesServices,
                                code: record.data.DescriptionOfSuppliesServices
                            });
                        } else if (record.data.Company === 'CJLR') {
                            editor5.getStore().add({
                                // id:record.data.id,
                                name: record.data.DescriptionOfSuppliesServices,
                                code: record.data.DescriptionOfSuppliesServices
                            });
                        }
                    } else {
                        if (record.data.Company === 'JLRC' || record.data.Company === 'LRE' || record.data.Company === 'UK') {
                            // var r = editor.getStore().getById(record.data.id);
                            var r = editor4.getStore().findRecord('code', record.modified.DescriptionOfSuppliesServices);
                            if (r) {
                                r.data.name = record.data.DescriptionOfSuppliesServices;
                                r.data.code = record.data.DescriptionOfSuppliesServices;

                                var store = grid4.getStore();
                                var item;
                                for (var i = 0; i < store.data.items.length; i++) {
                                    item = store.data.items[i].data;
                                    if (item.DescriptionOfSuppliesServices == record.modified.DescriptionOfSuppliesServices) {
                                        item.DescriptionOfSuppliesServices = record.data.DescriptionOfSuppliesServices;
                                        item.ProjectActivity = record.data.ProjectActivity;
                                        store.data.items[i].commit();
                                    }
                                }
                            }
                        } else if (record.data.Company === 'CJLR') {
                            // var r = editor.getStore().getById(record.data.id);
                            var r = editor5.getStore().findRecord('code', record.modified.DescriptionOfSuppliesServices);
                            if (r) {
                                r.data.name = record.data.DescriptionOfSuppliesServices;
                                r.data.code = record.data.DescriptionOfSuppliesServices;

                                var store = grid5.getStore();
                                var item;
                                for (var i = 0; i < store.data.items.length; i++) {
                                    item = store.data.items[i].data;
                                    if (item.DescriptionOfSuppliesServices == record.modified.DescriptionOfSuppliesServices) {
                                        item.DescriptionOfSuppliesServices = record.data.DescriptionOfSuppliesServices;
                                        item.ProjectActivity = record.data.ProjectActivity;
                                        store.data.items[i].commit();
                                    }
                                }
                            }
                        }
                    }
                } else if (record.modified.CompanySharingRatio !== undefined || record.modified.Cost !== undefined) {
                    if (!Ext.isEmpty(record.data.CompanySharingRatio)) {
                        var r = store2.findRecord('DescriptionOfSuppliesServices', record.data.DescriptionOfSuppliesServices);
                        if (r) {
                            record.data.Cost = (r.data.TotalPrice * record.data.CompanySharingRatio) / 100;
                        }

                        var map = new Map();
                        map.put(pro, 0);
                        map.put(pro + "#", budgetControl);

                        for (var i = 0; i < store3.data.items.length; i++) {
                            item = store3.data.items[i].data;
                            if (!Ext.isEmpty(record.data.Cost) && !Ext.isEmpty(item.ProjectActivity)) {
                                var companyval = item.Company;
                                var pro1 = item.ProjectActivity;
                                if (pro == pro1 && (companyval == "JLRC" || companyval == "LRE" || companyval == "UK")) {
                                    var cost = map.get(pro);
                                    var costinfo = floatAdd(cost, record.data.Cost);
                                    map.put(pro, costinfo);
                                    item.Balance = balance - costinfo;
                                }
                                if (companyval == "CJLR") {
                                    item.Balance = "N/A";
                                }

                                if (pro == pro1) {
                                    var budgetControls = map.get(pro + "#");
                                    item.budgetControl = budgetControls;
                                }
                            }
                        }

                        if (record.data.Company === 'JLRC') {
                            var store = grid4.getStore();
                            var item;
                            for (var i = 0; i < store.data.items.length; i++) {
                                item = store.data.items[i].data;
                                if (item.DescriptionOfSuppliesServices == record.data.DescriptionOfSuppliesServices) {
                                    item.Cost = (record.data.Cost * item.BrandSharingRatio) / 100;
                                    store.data.items[i].commit();
                                }
                            }
                        } else if (record.data.Company === 'CJLR') {
                            var store = grid5.getStore();
                            var item;
                            for (var i = 0; i < store.data.items.length; i++) {
                                item = store.data.items[i].data;
                                if (item.DescriptionOfSuppliesServices == record.data.DescriptionOfSuppliesServices) {
                                    item.Cost = (record.data.Cost * item.BrandSharingRatio) / 100;
                                    store.data.items[i].commit();
                                }
                            }
                        }
                    }

                } else if (record.modified.ProjectActivity !== undefined) {
                    if (record.data.Company === 'JLRC') {
                        var store = grid4.getStore();
                        var item;
                        for (var i = 0; i < store.data.items.length; i++) {
                            item = store.data.items[i].data;
                            if (item.ProjectActivity == record.modified.ProjectActivity && item.DescriptionOfSuppliesServices == record.data.DescriptionOfSuppliesServices) {
                                item.ProjectActivity = record.data.ProjectActivity;
                                store.data.items[i].commit();
                            }
                        }
                    } else if (record.data.Company === 'CJLR') {
                        var store = grid5.getStore();
                        var item;
                        for (var i = 0; i < store.data.items.length; i++) {
                            item = store.data.items[i].data;
                            if (item.ProjectActivity == record.modified.ProjectActivity && item.DescriptionOfSuppliesServices == record.data.DescriptionOfSuppliesServices) {
                                item.ProjectActivity = record.data.ProjectActivity;
                                store.data.items[i].commit();
                            }
                        }
                    }
                } else if (record.modified.Company !== undefined) {
                    // 控制Brand表格的显示和影藏
                    var item = [];
                    for (var i = 0; i < store.data.items.length; i++) {
                        item.push(store.data.items[i].data.Company);
                    }

                    if ((item.includes('JLRC') || item.includes('LRE')) && item.includes('CJLR')) {
                        ref.BrandAllocationJLRC.show();
                        ref.BrandAllocationCJLR.show();
                        ref.FinanceAnalystOwnerJLRC.show();
                        ref.FinanceAnalystOwnerJLRC.setAllowBlank(false);
                        ref.FinanceAnalystOwnerCJLR.show();
                        ref.FinanceAnalystOwnerCJLR.setAllowBlank(false);
                        ref.section39.show();
                    } else if (item.includes('CJLR') && (!item.includes('JLRC') && !item.includes('LRE'))) {
                        ref.BrandAllocationJLRC.hide();
                        ref.BrandAllocationCJLR.show();
                        ref.FinanceAnalystOwnerCJLR.show();
                        ref.FinanceAnalystOwnerCJLR.setAllowBlank(false);
                        ref.FinanceAnalystOwnerJLRC.hide();
                        ref.FinanceAnalystOwnerJLRC.setAllowBlank(true);
                        ref.section39.hide();
                    } else if ((item.includes('JLRC') || item.includes('LRE')) && !item.includes('CJLR')) {
                        ref.BrandAllocationJLRC.show();
                        ref.BrandAllocationCJLR.hide();
                        ref.FinanceAnalystOwnerJLRC.show();
                        ref.FinanceAnalystOwnerJLRC.setAllowBlank(false);
                        ref.FinanceAnalystOwnerCJLR.hide();
                        ref.FinanceAnalystOwnerCJLR.setAllowBlank(true);
                        ref.section39.show();
                    } else {
                        ref.BrandAllocationJLRC.hide();
                        ref.BrandAllocationCJLR.hide();
                        ref.FinanceAnalystOwnerJLRC.hide();
                        ref.FinanceAnalystOwnerJLRC.setAllowBlank(true);
                        ref.FinanceAnalystOwnerCJLR.hide();
                        ref.FinanceAnalystOwnerCJLR.setAllowBlank(true);
                        ref.section39.hide();
                    }
                    // 配置Brand表格中DescriptionOfSuppliesServices的下拉框
                    if (record.data.DescriptionOfSuppliesServices !== '') {
                        if (record.data.Company === 'JLRC') {
                            editor4.getStore().add({
                                name: record.data.DescriptionOfSuppliesServices,
                                code: record.data.DescriptionOfSuppliesServices
                            })
                        } else if (record.data.Company === 'CJLR') {
                            editor5.getStore().add({
                                name: record.data.DescriptionOfSuppliesServices,
                                code: record.data.DescriptionOfSuppliesServices
                            })
                        }

                        if (record.modified.Company === 'JLRC') {
                            var r = editor4.getStore().findRecord('code', record.data.DescriptionOfSuppliesServices);
                            editor4.getStore().remove(r);
                        } else if (record.modified.Company === 'CJLR') {
                            var r = editor5.getStore().findRecord('code', record.data.DescriptionOfSuppliesServices);
                            editor5.getStore().remove(r);
                        }
                    }

                } else if (record.modified.Cost !== undefined) { }
            }
            record.commit();
        } else if (flag == 'add') {
            record.commit();
        }
    }
}

function Map() {
    var struct = function (key, value) {
        this.key = key;
        this.value = value;
    };
    // 添加map键值对  
    var put = function (key, value) {
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                this.arr[i].value = value;
                return;
            }
        };
        this.arr[this.arr.length] = new struct(key, value);
    };
    //  根据key获取value   
    var get = function (key) {
        for (var i = 0; i < this.arr.length; i++) {
            if (this.arr[i].key === key) {
                return this.arr[i].value;
            }
        }
        return null;
    };
    //   根据key删除  
    var remove = function (key) {
        var v;
        for (var i = 0; i < this.arr.length; i++) {
            v = this.arr.pop();
            if (v.key === key) {
                continue;
            }
            this.arr.unshift(v);
        }
    };
    //   获取map键值对个数  
    var size = function () {
        return this.arr.length;
    };
    // 判断map是否为空    
    var isEmpty = function () {
        return this.arr.length <= 0;
    };
    this.arr = new Array();
    this.get = get;
    this.put = put;
    this.remove = remove;
    this.size = size;
    this.isEmpty = isEmpty;
}

function floatAdd(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length; } catch (e) { r1 = 0; }
    try { r2 = arg2.toString().split(".")[1].length; } catch (e) { r2 = 0; }
    m = Math.pow(10, Math.max(r1, r2));
    return (arg1 * m + arg2 * m) / m;
}
